package com.sam.library.document;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author wanghao
 * @date 2022/11/2.
 * @des
 */
public class XMLActivity extends AppCompatActivity {

    private String filePath;

    private static final String EXTRA_DATA = "data";

    public static void startActivity(Context context, String filePath) {
        if (context != null && filePath != null) {
            Intent intent = new Intent(context, XMLActivity.class);
            intent.putExtra(EXTRA_DATA, filePath);
            context.startActivity(intent);
        }
    }

    SharedPreferences preferences;

    FileAdapter mAdapter = new FileAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout._sam_activity_document);
        filePath = getIntent().getStringExtra(EXTRA_DATA);
        String fileName = new File(filePath).getName();
        preferences = getSharedPreferences(fileName.substring(0, fileName.lastIndexOf(".xml")), MODE_PRIVATE);
        Map<String, Object> map = (Map<String, Object>) preferences.getAll();

        RecyclerView recyclerView = findViewById(R.id.content);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(mAdapter);

        mAdapter.setData(map);
        mAdapter.setPreferences(preferences);


    }

    static class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements View.OnClickListener {

        private List<Map.Entry<String, ?>> mData;

        private SharedPreferences preferences;
        ;

        public void setPreferences(SharedPreferences preferences) {
            this.preferences = preferences;
        }

        public void setData(Map<String, Object> map) {
            Set<Map.Entry<String, Object>> set = map.entrySet();
            mData = new ArrayList<>(map.size());
            for (Map.Entry<String, Object> entry : set) {
                mData.add(entry);
            }
            notifyDataSetChanged();
        }

        @Override
        public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout._sam_xml_item, parent, false);
            view.setOnClickListener(this);
            return new FileViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
            holder.itemView.setTag(position);
            holder.textView.setText(mData.get(position).getKey() + " ->" + StringUtil.formatValue(mData.get(position).getValue()));
        }

        @Override
        public int getItemCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            final Map.Entry<String, Object> entry = (Map.Entry<String, Object>) mData.get(position);
            XMLEditDialog.showDialog(((FragmentActivity) v.getContext()).getSupportFragmentManager(), entry.getKey(), StringUtil.formatValue(entry.getValue()), new XMLEditDialog.OnEditListener() {
                @Override
                public void onEdit(String key, String value) {
                    if (preferences == null) {
                        return;
                    }
                    Object v = entry.getValue();
                    Object valueObject = null;
                    if (v instanceof String) {
                        valueObject = value;
                        preferences.edit().putString(key, value).apply();
                    } else if (v instanceof Integer) {
                        valueObject = Integer.parseInt(value);
                        preferences.edit().putInt(key, (Integer) valueObject).apply();
                    } else if (v instanceof Long) {
                        valueObject = Long.parseLong(value);
                        preferences.edit().putLong(key, (Long) valueObject).apply();
                    } else if (v instanceof Float) {
                        valueObject = Float.parseFloat(value);
                        preferences.edit().putFloat(key, (Float) valueObject).apply();
                    } else if (v instanceof Boolean) {
                        valueObject = Boolean.parseBoolean(value);
                        preferences.edit().putBoolean(key, (Boolean) valueObject).apply();
                    } else if (v instanceof Set) {
                        valueObject = StringUtil.stringToStringSet(value);
                        preferences.edit().putStringSet(key, (Set<String>) valueObject).apply();
                    }

                    entry.setValue(valueObject);
                    notifyItemChanged(position);

                }
            });

        }
    }

    static class FileViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public FileViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
        }
    }
}
